<?php

class Application_Model_Asset
{
    /**
     * @return AssetPOJO
     */
    public static function getRootAsset()
    {
        $dbAdapter = JDbAdapter::factory();

        $row = $dbAdapter->fetchRow(
            $dbAdapter->select()->from("ASSETS")->where('ID = 1')
        );

        return new AssetPOJO($row);
    }
}

class AssetPOJO extends POJO
{
    /** @var int */
    public $ID;

    /** @var string */
    public $Name;

    /** @var RulePOJO[] */
    public $Rules;

    /**
     * @return void
     */
    protected function convert()
    {
        $this->ID = (int)$this->ID;
        $this->Name = (string)$this->Name;
        $ruleData = json_decode($this->Rules, true);
        $this->Rules = array();
        foreach ($ruleData as $ruleName => $rulePermissions) {
            array_push($this->Rules, new RulePOJO($ruleName, $rulePermissions));
        }
    }
}

class RulePOJO
{
    /** @var string */
    public $Name;

    /** @var int[] */
    public $Permissions;

    /**
     * @param string $name
     * @param int[] $permissions
     */
    public function __construct($name, $permissions)
    {
        $this->Name = $name;
        $this->Permissions = $permissions;
    }
}
